package com.zjd.association.controller;

import com.zjd.association.common.PageWrapper;
import com.zjd.association.common.R;
import com.zjd.association.entity.Teams;
import com.zjd.association.entity.Users;
import com.zjd.association.handle.CacheHandle;
import com.zjd.association.service.TeamsService;
import com.zjd.association.service.UsersService;
import com.zjd.association.utils.DateUtils;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 活动信息 控制层
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/teams")
public class TeamsController {

    private final CacheHandle cacheHandle;

    private final UsersService usersService;

    private final TeamsService teamsService;

    /**
     * 获取路径
     *
     * @return 路径
     */
    @RequestMapping("")
    public String index() {
        return "pages/Teams";
    }

    /**
     * 获取活动信息
     *
     * @param id 活动i
     * @return 活动信息
     */
    @GetMapping("/info")
    public R getInfo(String id) {
        Teams teams = teamsService.getOne(id);
        return R.ok(teams);
    }

    /**
     * 获取活动信息 全部
     *
     * @return 活动信息
     */
    @GetMapping("/all")
    public R getAll() {
        List<Teams> list = teamsService.getAll();
        return R.ok(list);
    }

    /**
     * 获取管理员的活动
     *
     * @param manId 管理员id
     * @return 活动信息
     */
    @GetMapping("/man")
    public R getListByManId(String manId) {
        List<Teams> list = teamsService.getListByManId(manId);
        return R.ok(list);
    }

    /**
     * 查询活动信息 分页
     *
     * @param pageIndex 分页页数
     * @param pageSize  分页大小
     * @param token     用户token
     * @param teams     团队名
     * @return 活动信息
     */
    @GetMapping("/page")
    public R getPageInfos(
            Long pageIndex, Long pageSize,
            String token, Teams teams
    ) {
        //获取用户信息
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        if (user.getType() == 1) {
            //管理员
            teams.setManager(user.getId());
        }
        PageWrapper page = teamsService.getPageInfo(pageIndex, pageSize, teams);
        return R.ok(page);
    }

    /**
     * 添加活动信息
     *
     * @param teams 活动信息
     * @return 结果
     */
    @PostMapping("/add")
    public R addInfo(Teams teams) {
        teams.setId(UUIDUtils.makeIDByCurrent());
        teams.setCreateTime(DateUtils.getNowDate("yyyy-MM-dd"));
        teamsService.add(teams);
        return R.ok();
    }

    /**
     * 更新活动信息
     *
     * @param teams 活动信息
     * @return 结果
     */
    @PostMapping("/upd")
    public R updInfo(Teams teams) {
        teamsService.update(teams);
        return R.ok();
    }

    /**
     * 删除活动信息
     *
     * @param id 活动id
     * @return 结果
     */
    @PostMapping("/del")
    public R delInfo(String id) {
        Teams teams = teamsService.getOne(id);
        teamsService.delete(teams);
        return R.ok();
    }

}